# Customise this file, documentation can be found here:
# https://github.com/KrauseFx/fastlane/tree/master/docs
# vi: ft=ruby

$:.unshift File.dirname(__FILE__)
require 'lib/utils.rb'

fastlane_version "1.3"

default_platform :ios

platform :ios do
  before_all do
     ENV['DELIVER_WHAT_TO_TEST'] = git_commit_log
     ensure_git_status_clean
  end

  desc "Runs linting (and eventually static analysis)"
  lane :analyze do
    make 'lint' unless test_disabled?
  end

  desc "Runs unit tests, optionally generating a JUnit report."
  lane :verify do |options|
    if ENV['TRAVIS'] then
      xctest(
        scheme: 'Wikipedia',
        workspace: 'Wikipedia.xcworkspace',
        destination: 'platform=iOS Simulator,name=iPhone 6,OS=9.0'
      )
    else
      # only generate junit reports on jenkins
      xctool :test, '-reporter junit:build/reports/unit-tests.xml -reporter pretty'
    end
  end

  desc "Increment the app's build number and commit the changes."
  lane :bump do |options|
    opt_build_num = options[:build_number] || ENV['BUILD_NUMBER']
    if opt_build_num then
      increment_build_number(build_number: opt_build_num.to_i)
    else
      increment_build_number
    end
    commit_version_bump
    add_git_tag(tag: "#{get_version_number}.#{get_build_number}")
  end

  desc "Submit a new **Wikipedia Alpha** Build to Apple TestFlight"
  lane :alpha do
    sigh(
      adhoc: false,
      force: true
    )

    gym(
      configuration: "Alpha",
      scheme: "Wikipedia Alpha"
    )

    hockey(
      notes: git_commit_log,
      notify: '0', # Means do not notify
      status: '1', # Means do not make available for download
    )

    pilot(skip_submission: true, ipa: ENV['IPA_OUTPUT_PATH'])
  end

  desc "Submit a new **Wikipedia Beta** build to Apple TestFlight"
  lane :beta do
    sigh(
      adhoc: false,
      force: true,
      filename: 'build/beta.mobileprovision'
    )
    ipa(
      configuration: "Beta",
      scheme: "Wikipedia Beta",
      embed: 'build/beta.mobileprovision',
    )
    hockey(
      notes: git_commit_log,
      notify: '0', # Means do not notify
      status: '1', # Means do not make available for download
    )
    pilot(skip_submission: false)
  end

  desc "Upload a developer build to Hockey."
  lane :dev do
    sigh(
      adhoc: true,
      # Fastlane has issues forcing AdHoc profiles
      force: false
    )

    # force iTunes file sharing to be enabled (normally disabled for release builds)
    ENV['WMF_FORCE_ITUNES_FILE_SHARING'] = '1'
    # force debug menu to be shown
    ENV['WMF_FORCE_DEBUG_MENU'] = '1'

    gym(
      configuration: "AdHoc",
      scheme: "Wikipedia AdHoc",
      # both of these flags are required for ad hoc
      export_method: 'ad-hoc',
      use_legacy_build_api: true
    )

    hockey(
      notes: git_commit_log,
      notify: '2', # Notify all testers
      status: '2', # Make available for download
      release_type: '2' # 'alpha' release type
    )
  end


  desc "Upload AdHoc \"Release Candidate\" build to Hockey."
  lane :rc do
    sigh(
      adhoc: true,
      force: true,
      filename: 'build/adhoc.mobileprovision'
    )
    # force iTunes file sharing to be enabled (normally disabled for release builds)
    ENV['WMF_FORCE_ITUNES_FILE_SHARING'] = '1'
    # force debug menu to be shown
    ENV['WMF_FORCE_DEBUG_MENU'] = '1'
    ipa(
      configuration: "Release",
      scheme: "Wikipedia",
      embed: 'build/adhoc.mobileprovision',
    )
    hockey(
      notes: git_commit_log,
      notify: '0', # Do not notify
      status: '2', # Make available for download
      release_type: '0' # 'beta' release type
    )
  end

  desc "Upload AppStore build to iTC & HockeyApp"
  lane :store do
    sigh(
      adhoc: false,
      force: true,
      filename: 'build/release.mobileprovision'
    )
    ipa(
      configuration: "Release",
      scheme: "Wikipedia",
      embed: 'build/release.mobileprovision',
    )
    hockey(
      notes: git_commit_log,
      notify: '0', # Do not notify
      status: '1', # Do not make available for download
      release_type: '1' # 'store' release type
    )
    deliver force: true
  end

  after_all do |lane|

  end

  error do |lane, exception|

  end
end
